package net.yla.smartbus.module.controller.web.ba.group;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.constants.UserRoleEnum;
import net.yla.smartbus.module.ba.group.entity.GroupEntity;
import net.yla.smartbus.module.ba.group.query.CompanyQuery;
import net.yla.smartbus.module.ba.group.query.GroupQuery;
import net.yla.smartbus.module.ba.group.service.GroupService;
import net.yla.smartbus.module.ba.right.roles.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: Yee
 * @date: 2021/6/18 16:46
 */
@RestController
@RequestMapping("/api/web/group")
public class GroupController extends BaseController {

    @Autowired
    private GroupService groupService;
    @Autowired
    private RolesService rolesService;

    /**
     * 级联菜单
     *
     * @return
     */
    @PostMapping("/cascader")
    public ApiResult getCascader() {
        return success(groupService.trees());
    }

    /**
     * 获取机构信息
     *
     * @param query
     * @return
     */
    @PostMapping("/getGroups")
    public ApiResult getGroups(@RequestBody GroupQuery query) {
       rolesService.userRoleEvaluation(UserRoleEnum.GROUP_SELECT,getToken(),getIpAddress());
        return success(groupService.getGroups(query));
    }

    /**
     * 删除机构信息
     *
     * @param query
     * @return
     */
    @PostMapping("/delete")
    public ApiResult deleteGroups(@RequestBody GroupQuery query) {
        rolesService.userRoleEvaluation(UserRoleEnum.GROUP_DELETE, getToken(), getIpAddress());
        return success(groupService.deleteGroups(query));
    }

    @PostMapping("/edit")
    public ApiResult editCompany(@RequestBody CompanyQuery query) {
        if (query.getCompanyId() != null) {
            rolesService.userRoleEvaluation(UserRoleEnum.GROUP_UPDATE, getToken(), getIpAddress());
        } else {
            rolesService.userRoleEvaluation(UserRoleEnum.GROUP_INSERT, getToken(), getIpAddress());
        }
        return success(groupService.editCompany(query));
    }


    /**
     * 根据id 查询 分组
     *
     * @return
     */
    @PostMapping("/mcl/view")
    public ApiResult view(Integer groupId) {
        GroupEntity view = groupService.view(groupId);
        return success(view);
    }


}
